<template>
  <div style="width: 80%;">
    <div style="margin-bottom: 30px;margin-left: 30px"><h2>新增用户</h2></div>
    <el-form :inline="true" :model="form" :rules="rules" ref="ruleForm" label-width="100px" style="margin-left: 110px;"
             size="max">
      <el-form-item label="姓名" prop="name">
        <el-input prefix-icon="el-icon-user" v-model="form.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input prefix-icon="el-icon-warning-outline" v-model="form.age" placeholder="请输入年龄"></el-input>
      </el-form-item>

      <el-form-item label="性别">
        <el-radio v-model="form.gender" label="男">男</el-radio>
        <el-radio v-model="form.gender" label="女">女</el-radio>
      </el-form-item>

      <el-form-item label="联系方式" prop="phone">
        <el-input prefix-icon="el-icon-phone-outline" v-model="form.phone" placeholder="请输入联系方式"></el-input>
      </el-form-item>
      <el-form-item label="地址">
        <el-input prefix-icon="el-icon-location-information" v-model="form.address" placeholder="请输入地址"></el-input>
      </el-form-item>
    </el-form>

    <div style="text-align: center;margin-top: 20px">
      <el-button type="primary" @click="save" size="medium">提交</el-button>
    </div>
  </div>
</template>
<script>
import request from "@/utils/request";

export default {
  name: "AddUser",
  data() {
    const checkAge = (rule, value, callback) => {   //校验年龄规则
      if (!value) {
        return callback(new Error('年龄不能为空！'));
      }
      if (!/^[0-9]+$/.test(value)) {
        callback(new Error('请输入数字值！'));
      }
      if (parseInt(value) > 130 || parseInt(value) <= 0) {
        callback(new Error('请输入合理的年龄！'))
      }
      callback()
    };
    const checkPhone = (rule, value, callback) => {   //校验联系方式规则
      if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(value)) {
        callback(new Error('请输入合法的联系方式！'));
      }
      callback()
    };
    return {
      form: {
        gender: '男',
      },
      rules: {
        name: [
          {required: true, message: '姓名不能为空', trigger: 'blur'},
          {min: 2, max: 7, message: '长度在 2 到 7 个字符', trigger: 'blur'}
        ],
        age: [
          {required: true, message: '年龄不能为空', trigger: 'blur'},
          {validator: checkAge, trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '联系方式不能为空', trigger: 'blur'},
          {validator: checkPhone, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request.post('/user/save', this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success('新增成功！')
              this.form = {}        //新增完成清空输入框内容
              this.$router.push("/user")   //跳转到列表界面
              // this.$refs['ruleForm'].resetFileds()  //清空表单
            } else {
              this.$notify.error('新增失败！' + res.msg)
            }
          })
        }
      })
    }
  }
}
</script>